package com.example.demo.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.Entity.Directory;
import com.example.demo.Entity.Directoryitem;
import com.example.demo.Entity.Note;
import com.example.demo.Mapper.DirectoryMapper;
import com.example.demo.Mapper.NoteMapper;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DirectoryServiceImp implements DirectoryService{
    @Autowired
    private DirectoryMapper directoryMapper;
    @Autowired
    private NoteMapper noteMapper;

    /* 查找相应条件下的笔记 */
    public QueryWrapper<Note> getNoteQueryWrapper(QueryWrapper<Directory> dirs){
        //得到相应条件下的笔记编号数组
        List<Integer> ids = directoryMapper.selectList(dirs)
                .stream()
                .map(Directory::getNoteId)
                .toList();

        return new QueryWrapper<Note>().in("note_id",ids);
    }

    /** 查询一级目录下的所有笔记 */
    @Override
    public List<Note> select(Integer group){
        QueryWrapper<Directory> dirs = new QueryWrapper<Directory>()
                .eq("directory_group", group);

        QueryWrapper<Note> wrapper = getNoteQueryWrapper(dirs);
        return noteMapper.selectList(wrapper);
    }

    /** 查询二级目录下的所有笔记 */
    @Override
    public  List<Note>select(Integer group,Integer subGroup){
        QueryWrapper<Directory> dirs = new QueryWrapper<Directory>()
                .eq("directory_group", group)
                .eq("sub_group",subGroup);

        QueryWrapper<Note> wrapper = getNoteQueryWrapper(dirs);
        return noteMapper.selectList(wrapper);
    }

    /** 查询二级目录下的对应ID笔记 */
    @Override
    public  List<Note>select(Integer group,Integer subGroup,Integer subId){
        QueryWrapper<Directory> dirs = new QueryWrapper<Directory>()
                .eq("directory_group", group)
                .eq("sub_group",subGroup)
                .eq("sub_id",subId);

        QueryWrapper<Note> wrapper = getNoteQueryWrapper(dirs);
        return noteMapper.selectList(wrapper);
    }

    /** 删除一级目录(级联) */
    @Override
    public void deleteByGroup(Integer group){
        QueryWrapper<Directory> dirs = new QueryWrapper<Directory>()
                .eq("directory_group",group);
        QueryWrapper<Note> notes = getNoteQueryWrapper(dirs);
        directoryMapper.delete(dirs);
        noteMapper.delete(notes);
    }

    /** 删除二级目录(级联) */
    @Override
    public void deleteBySubGroup(Integer group,Integer subGroup){
        QueryWrapper<Directory> dirs = new QueryWrapper<Directory>()
                .eq("directory_group",group)
                .eq("sub_group",subGroup);
        QueryWrapper<Note> notes = getNoteQueryWrapper(dirs);
        directoryMapper.delete(dirs);
        noteMapper.delete(notes);
    }

    /** 删除子笔记 */
    @Override
    public void deleteBySubId(Integer group,Integer subGroup,Integer subId){
        QueryWrapper<Directory> dirs = new QueryWrapper<Directory>()
                .eq("directory_group",group)
                .eq("sub_group",subGroup)
                .eq("sub_id",subId);
        QueryWrapper<Note> notes = getNoteQueryWrapper(dirs);
        directoryMapper.delete(dirs);
        noteMapper.delete(notes);
    }

    /** 更新目录 */
    @Override
    public void update(Directory dir){
        directoryMapper.insertOrUpdate(dir);
    }

    /** 插入目录 */
    @Override
    public void insert(Directory dir) throws Exception {
        QueryWrapper<Directory> dirs = new QueryWrapper<Directory>()
                .eq("directory_group",dir.getDirectoryGroup())
                .eq("user_id",dir.getUserId())
                .eq("note_id",dir.getNoteId());
        if(directoryMapper.exists(dirs)){
            throw new Exception("wdnmd");
        }
        directoryMapper.insert(dir);
    }

    /***/
    @Override
    public void deleteByNoteId(Integer noteId){
        directoryMapper.deleteByNoteId(noteId);
    }


}